package com.common.atom.seal.util;

import com.common.atom.seal.param.TriangleSealParam;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * description: TriangleUtil <br>
 * date: 2023/4/14  <br>
 * author: zss <br>
 * version: 1.0.0 <br>
 */
public class TriangleUtil {

    public static void main(String[] args) {
        String s = "[{\"crossSignControls\":[{\"height\":160,\"id\":2,\"key\":\"L01XYT2BQVDKTF-1646753878793310209-2\",\"pageNum\":\"0\",\"type\":\"cross\",\"value\":\"UjnHSe7PPr5w1vylHUI9PB4xdKEHeP+IohM3AXSj6K4=\",\"value1\":\"base64:***\",\"width\":80,\"x\":0.90476,\"y\":0.86521},{\"height\":160,\"id\":5,\"key\":\"L01XYT2BQVDKTF-1646753878793310209-3\",\"pageNum\":\"0\",\"type\":\"cross\",\"value\":\"uDyUPzHqtecctGC11c5gTBwL7MQ6T5y6khipy6W5eVA=\",\"value1\":\"base64:***\",\"width\":80,\"x\":0.90476,\"y\":0.11036}],\"docId\":1646753878793310209,\"docType\":true,\"xySignControls\":[{\"attr\":{},\"height\":113.39,\"id\":1,\"key\":\"\",\"pageNum\":\"1\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"signet\",\"value\":\"uDyUPzHqtecctGC11c5gTBwL7MQ6T5y6khipy6W5eVA=\",\"value1\":\"base64:***\",\"width\":113.39,\"x\":0.0,\"y\":0.80708},{\"attr\":{},\"height\":40.0,\"id\":3,\"key\":\"\",\"pageNum\":\"1\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"agent\",\"value\":\"4e6OtbCt3Sgcn8RYUEadvzbZMpEraNwWlAljn5DJC6k=\",\"value1\":\"base64:***\",\"width\":82.0,\"x\":0.31786,\"y\":0.94187},{\"attr\":{},\"height\":22.0,\"id\":4,\"key\":\"\",\"pageNum\":\"1\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"date\",\"value\":\"20NDL2wyr5nQx38sABMWmg==\",\"value1\":\"base64:***\",\"width\":122.0,\"x\":0.51667,\"y\":0.95451},{\"attr\":{},\"height\":113.39,\"id\":2,\"key\":\"\",\"pageNum\":\"2\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"signet\",\"value\":\"uDyUPzHqtecctGC11c5gTBwL7MQ6T5y6khipy6W5eVA=\",\"value1\":\"base64:***\",\"width\":113.39,\"x\":0.05952,\"y\":0.05392},{\"attr\":{},\"height\":40.0,\"id\":4,\"key\":\"\",\"pageNum\":\"2\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"agent\",\"value\":\"4e6OtbCt3Sgcn8RYUEadvzbZMpEraNwWlAljn5DJC6k=\",\"value1\":\"base64:***\",\"width\":82.0,\"x\":0.6631,\"y\":0.13901},{\"attr\":{},\"height\":22.0,\"id\":5,\"key\":\"\",\"pageNum\":\"2\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"date\",\"value\":\"20NDL2wyr5nQx38sABMWmg==\",\"value1\":\"base64:***\",\"width\":122.0,\"x\":0.66429,\"y\":0.27801}]},{\"crossSignControls\":[{\"height\":160,\"id\":2,\"key\":\"L01XYT2BQVDKTF-1646753878847836161-2\",\"pageNum\":\"0\",\"type\":\"cross\",\"value\":\"uDyUPzHqtecctGC11c5gTBwL7MQ6T5y6khipy6W5eVA=\",\"value1\":\"base64:***\",\"width\":80,\"x\":0.90476,\"y\":0.43976},{\"height\":160,\"id\":5,\"key\":\"L01XYT2BQVDKTF-1646753878847836161-3\",\"pageNum\":\"0\",\"type\":\"cross\",\"value\":\"UjnHSe7PPr5w1vylHUI9PB4xdKEHeP+IohM3AXSj6K4=\",\"value1\":\"base64:***\",\"width\":80,\"x\":0.90476,\"y\":0.27464}],\"docId\":1646753878847836161,\"docType\":true,\"xySignControls\":[{\"attr\":{},\"height\":113.39,\"id\":1,\"key\":\"\",\"pageNum\":\"1\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"signet\",\"value\":\"uDyUPzHqtecctGC11c5gTBwL7MQ6T5y6khipy6W5eVA=\",\"value1\":\"base64:***\",\"width\":113.39,\"x\":0.66548,\"y\":0.20725},{\"attr\":{},\"height\":40.0,\"id\":3,\"key\":\"\",\"pageNum\":\"1\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"agent\",\"value\":\"4e6OtbCt3Sgcn8RYUEadvzbZMpEraNwWlAljn5DJC6k=\",\"value1\":\"base64:***\",\"width\":82.0,\"x\":0.63095,\"y\":0.40944},{\"attr\":{},\"height\":22.0,\"id\":4,\"key\":\"\",\"pageNum\":\"1\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"date\",\"value\":\"20NDL2wyr5nQx38sABMWmg==\",\"value1\":\"base64:***\",\"width\":122.0,\"x\":0.60357,\"y\":0.53412},{\"attr\":{},\"height\":40.0,\"id\":3,\"key\":\"\",\"pageNum\":\"2\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"agent\",\"value\":\"4e6OtbCt3Sgcn8RYUEadvzbZMpEraNwWlAljn5DJC6k=\",\"value1\":\"base64:***\",\"width\":82.0,\"x\":0.59643,\"y\":0.18534},{\"attr\":{},\"height\":40.0,\"id\":4,\"key\":\"\",\"pageNum\":\"2\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"agent\",\"value\":\"4e6OtbCt3Sgcn8RYUEadvzbZMpEraNwWlAljn5DJC6k=\",\"value1\":\"base64:***\",\"width\":82.0,\"x\":0.79643,\"y\":0.19292},{\"attr\":{},\"height\":113.39,\"id\":5,\"key\":\"\",\"pageNum\":\"2\",\"parentHeight\":1187.0,\"parentWidth\":840.0,\"type\":\"signet\",\"value\":\"uDyUPzHqtecctGC11c5gTBwL7MQ6T5y6khipy6W5eVA=\",\"value1\":\"base64:***\",\"width\":113.39,\"x\":0.57738,\"y\":0.31424}]}]";
        System.out.println(s.length());
    }


    /**
     * 画三角形印章
     */
    public static byte[] drawTriangleSeal(TriangleSealParam triangleSealDTO)
            throws IOException {
        Color color = ColorUtils.getColor(triangleSealDTO.getColor(), triangleSealDTO.getAlpha());

        Font font = new Font(getFontName(triangleSealDTO), Font.BOLD, 30);

        int width = 400, height = 400, padding = 5;
        // 创建图片对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        // 基于图片对象打开绘图
        Graphics2D graphics = image.createGraphics();
        // 设置画笔颜色
        graphics.setColor(color);

        drawTriangleSeal(width, height, padding, triangleSealDTO.getCompanyName(), triangleSealDTO.getHorizontalText(), graphics, font);

        // 处理绘图
        graphics.dispose();
        // 将绘制好的图片写入到图片
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", outputStream);

        return outputStream.toByteArray();
    }

    /**
     * 获取字体
     */
    private static String getFontName(TriangleSealParam triangleSealDTO) {
        String fontName;
        int fontType = triangleSealDTO.getFontType();
        if (fontType == 1) {
            fontName = "Microsoft YaHei";
        } else if (fontType == 2) {
            fontName = "SimHei";
        } else if (fontType == 3) {
            fontName = "SimSun";
        } else if (fontType == 4) {
            fontName = "华文行楷";
        } else if (fontType == 5) {
            fontName = "方正舒体";
        } else {
            fontName = "Microsoft YaHei";
        }
        return fontName;
    }

    /**
     * 画三角印章
     */
    private static void drawTriangleSeal(int width, int height, int padding, String name, String tag, Graphics2D graphics, Font font) {
        // 消除画图锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int sideLength = width - padding * 2;
        int triangleHeight = (int) Math.sqrt(Math.pow(sideLength, 2) - Math.pow(sideLength / 2F, 2));

        int x1 = padding;
        int y1 = (height - triangleHeight) / 2;

        int x2 = width - padding;
        int y2 = y1;

        int x3 = width / 2;
        int y3 = triangleHeight + y1;

        drawTriangle(x1 + 2, y1, x2 - 2, y2, x3, y3, graphics);

        y1 = drawName(x1, x2, y1, padding * 10, padding, name.trim(), graphics, font);
        y1 = drawRectangle(x1, x2, y1, padding * 18, graphics);
        if (tag != null && !"".equals(tag.trim())) {
            drawCenterName(x1, x2, y1, padding * 25, padding, tag.trim(), graphics, font);
        }
    }

    /**
     * 画水平文字
     */
    private static void drawCenterName(int x1, int x2, int y1, int length, int padding, String value, Graphics2D graphics, Font font) {
        // 消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        int height = (int) Math.sqrt(Math.pow(length * 2, 2) - Math.pow(length, 2));

        x1 = x1 + length;
        x2 = x2 - length;

        int lineWordCount = 16;

        if (value.length() <= lineWordCount) {
            height /= 3;
            graphics.setFont(font);
            drawAlignCenterString(x1, x2, y1, padding, value, graphics, height, font);
        } else {
            throw new RuntimeException("字数超过限制，最大为" + lineWordCount + "字");
        }
    }

    /**
     * 画水平和垂直居中文字
     */
    private static void drawAlignCenterString(int x1, int x2, int y1, int padding, String value, Graphics2D graphics, int height, Font font) {
        //设置字体
        graphics.setFont(font);
        //设置居中
        FontMetrics fm = graphics.getFontMetrics(font);
        int textWidth = fm.stringWidth(value);
        int ascent = fm.getAscent();
        int descent = fm.getDescent();

        int rectangleWidth = x2 - x1 - padding;
        if (textWidth > rectangleWidth) {
            double sx = (double) rectangleWidth / textWidth;
            double sy = (double) textWidth / rectangleWidth;

            AffineTransform affineTransform = new AffineTransform();
            affineTransform.scale(sx, sy);
            graphics.setFont(font.deriveFont(affineTransform));

            textWidth = (int) (textWidth * sx);
            ascent = ascent + (int) (ascent * sy) / 2;
        }

        graphics.drawString(value, x1 + (x2 - x1) / 2 - textWidth / 2, y1 + height / 2 + (ascent - descent) / 2);
    }

    /**
     * 画方框
     */
    private static int drawRectangle(int x1, int x2, int y1, int length, Graphics2D graphics) {
        // 消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.setStroke(new BasicStroke(4));

        int height = (int) Math.sqrt(Math.pow(length * 2, 2) - Math.pow(length, 2));
        int y2 = y1 + height / 2 - 16;
        y1 = y1 + 16;

        x1 = x1 + length;
        x2 = x2 - length;

        graphics.drawLine(x1, y1, x2, y1);
        graphics.drawLine(x1, y1, x1, y2);
        graphics.drawLine(x1, y2, x2, y2);
        graphics.drawLine(x2, y1, x2, y2);

        return y2;
    }

    /**
     * 画上旋文
     */
    private static int drawName(int x1, int x2, int y1, int length, int padding, String value, Graphics2D graphics, Font font) {
        // 消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        int height = (int) Math.sqrt(Math.pow(length * 2, 2) - Math.pow(length, 2));
        int y2 = height + y1;

        x1 = x1 + length;
        x2 = x2 - length;

        int lineWordCount = 36;
        if (value.length() <= lineWordCount) {
            drawAlignCenterString(x1, x2, y1, padding, value, graphics, height, font);
        } else {
            throw new RuntimeException("字数超过限制，最大为" + lineWordCount + "字");
        }

        return y2;
    }

    /**
     * 画三个点
     */
    private static void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, Graphics2D graphics) {
        graphics.setStroke(new BasicStroke(8));

        Polygon filledPolygon = new Polygon();
        filledPolygon.addPoint(x1, y1);
        filledPolygon.addPoint(x2, y2);
        filledPolygon.addPoint(x3, y3);

        graphics.drawPolygon(filledPolygon);
    }

}
